package com.md01.controller;

import com.md01.entity.Book;
import com.md01.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@ResponseBody
@RequestMapping(produces = "application/json;charset=utf-8")
public class Book1Controller {
    //1、先写注解
    //2、再写自动装配
    //3、把BookServiceImpl代码复制，删除注解
    //4、再把int类型改为String类型
    @Autowired
    private BookService bookService;

    @RequestMapping("/getAllBooks")
    public List<Book> getList() {
        List<Book> bookList = bookService.selectAllBooks();
        return bookList;
    }

    @RequestMapping("/selectBookById")
    public Book selectBookById(int id) {
        Book book = bookService.selectBookById(id);
        return book;
    }

    // 根据编号删除图书
    @RequestMapping("/deleteBookById")
    public String deleteBookById(int id) {
        int i = bookService.deleteBookById(id);
        return i > 0 ? "成功" : "失败";
    }

    @RequestMapping("insertBook")
    public String insertBook(@RequestBody Book book) {
        return bookService.insertBook(book) > 0 ? "成功" : "失败";
    }

    @RequestMapping("updateBook")
    public String updateBook(@RequestBody Book book) {
        return bookService.updateBook(book) > 0 ? "成功" : "失败";
    }

    // 根据书名查询图书
    @RequestMapping("selectBookByBookName")
    public List<Book> selectBookByBookName(String bookName) {
        return bookService.selectBookByBookName(bookName);
    }


}
